<?php
	ini_set("include_path", ini_get("include_path") . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . "/Common");
	ob_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Living Word Baptist Church</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<link href="/LivingWeb/templates/admin/text.css" rel="stylesheet" type="text/css">
	</head>
	<body>
	<?php
		include 'LivingWeb.conf.php';
		if(isset($_GET['login']) && $_GET['login'] == 'yes')
		{
			//Check to see if there is a valid session
			global $SSO,$GlobalSettings;
	?>	
<table cellpadding="0" cellspacing="0" width="100%">
	<tr>
		<td align="center">
			<?php 	
				global $SSO;	
				print $SSO->handleLoginError();
			 ?>			
		</td>
	</tr>
	<tr>
		<td align="center">
			<?php 		
				print $SSO->createLoginPrompt("index.php?action=main");
			 ?>
		</td>
	</tr>
</table>			
	<?php
			exit();
		}
	?>
		<script src="/CommonClientScripts/BrowserCookies.js" type="text/javascript"></script>
		<script type="text/javascript">
			function setMIDCookie(MID) {
				var objBrowserCookies = new BrowserCookies;

				objBrowserCookies.setCookie('MID', MID, false, '/');
			}
		</script>

		<form action="" method="post" name="frmChooseMID">
		<table width="100%">
			<tr>
				<td><br><br><br></td>
			</tr>
			<tr>
				<td align="center">
					<span class="T1">Please select the ministry for which you would like to browse for files.</span>
				</td>
			</tr>
			<tr>
				<td align="center"><br><br><br>
				<?php
					//Check to see if there is a valid session
					$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
				
					//Check to see if the user has rights to be on LivingWeb
					$PageRights = array('ADMIN', 'USER');
					$UserRoles = $Security->ValidateUser($SessionInfo['username'], 'LIVINGWEB', $PageRights, 1);
					
					//Grab the user info
					$UserInfo = $User->GetUserInfo($SessionInfo['username']);
					
					if(in_array("ADMIN",$UserRoles))
					{
						$DB->query("SELECT MinistryID, MinistryName FROM ministries ORDER BY MinistryName");
						print '<select name="selMinistryName" class="T1" onchange="setMIDCookie(this.options[this.selectedIndex].value);">';
						while($row = $DB->fetch_row())
						{
							print '<option value="' . $row['MinistryID'] . '" >' . $row['MinistryName'] . '</option>';
						}
						print '</select>';
					}
					else
					{
						$div_codes = split(',',$UserInfo['div_codes']);
						
						if(trim($div_codes[0]) == '')
						{
							array_pop($div_codes);
						}
						if(count($div_codes) <= 0)
						{
							print '<span class="T1" style="color:red;"><em>You do not have access to any ministries.  Contact your adminsitrator if you believe this to be an error.</em></span>';
						}
						else
						{
							$count = 0;
							sort($div_codes);
							global $DB;
							print '<select name="selMinistryName" class="T1" onchange="setMIDCookie(this.options[this.selectedIndex].value);">';
							while($count < count($div_codes))
							{
								$code = $div_codes[$count];
								$DB->query("SELECT MinistryID, MinistryName FROM ministries WHERE MinistryID = '" . $code . "'");
								$row = $DB->fetch_row();
								print '<option value="' . $row['MinistryID'] . '">' . $row['MinistryName'] . '</option>';
								$count = $count + 1;
							}
							print '</select>';
						}
					}
				?>
				</td>
			</tr>
			<tr>
				<td align="center">
					<input type="button" onClick="location.href='/LivingWeb/CMS/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php';" value="Open File Browser" class="B">
				</td>
			</tr>
		</table>
		</form>
		<script language="javascript1.2" type="text/javascript">
			setMIDCookie(frmChooseMID.selMinistryName.options[frmChooseMID.selMinistryName.selectedIndex].value);
		</script>
	</body>
</html>

<?php
	//flush all buffered output to the browser and stop buffering
	ob_end_flush();
?>